using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Globalization;

namespace Yorrick.FlickrMetadataSynchr.Log
{
   /// <summary>
   /// Class that derives from <see cref="TraceListener"/> to catch tracing information
   /// and redirects it to a very simple listener.
   /// </summary>
   public class ActivityTraceListener : TraceListener
   {
      #region Private fields
      /// <summary>
      /// This simple listener to use. Might be <c>null</c>.
      /// </summary>
      private IActivityListener _activityListener;
      #endregion

      #region Public methods
      /// <summary>
      /// Override to prevent extra information from being shown.
      /// </summary>
      /// <param name="eventCache"></param>
      /// <param name="source"></param>
      /// <param name="eventType"></param>
      /// <param name="id"></param>
      /// <param name="format"></param>
      /// <param name="args"></param>
      public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
      {
         WriteLine(String.Format(CultureInfo.InvariantCulture, format, args), eventType);
      }

      /// <summary>
      /// Override to prevent extra information from being shown.
      /// </summary>
      /// <param name="eventCache"></param>
      /// <param name="source"></param>
      /// <param name="eventType"></param>
      /// <param name="id"></param>
      /// <param name="message"></param>
      public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
      {
         WriteLine(message, eventType);
      }

      /// <summary>
      /// Writes the informational message to the simple listener if any is registered.
      /// </summary>
      /// <param name="message">The message to write.</param>
      public override void Write(string message)
      {
         WriteLine(message);
      }

      /// <summary>
      /// Writes the informational message to the simple listener if any is registered.
      /// </summary>
      /// <param name="message">The message to write.</param>
      public override void WriteLine(string message)
      {
         WriteLine(message, TraceEventType.Information);
      }

      /// <summary>
      /// Writes the message with the specified type to the simple listener if any is registered.
      /// </summary>
      /// <param name="message">The message to write.</param>
      public void WriteLine(string message, TraceEventType eventType)
      {
         /* Ignore any messages about the FlickrAPI cache. */
         if (message.IndexOf("cache", StringComparison.OrdinalIgnoreCase) != -1)
         {
            return;
         }

         if (_activityListener != null)
         {
            _activityListener.TraceActivity(message, eventType);
         }
      }
      #endregion

      #region Constructor
      /// <summary>
      /// Constructs an instance.
      /// </summary>
      /// <param name="activityListener">The simple activity listener to use.</param>
      internal ActivityTraceListener(IActivityListener activityListener)
      {
         _activityListener = activityListener;
      }
      #endregion
   }
}
